package com.panshi.modules.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.panshi.modules.service.VerifyCodeService;
import com.panshi.utils.RandomCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements VerifyCodeService {

    @Resource
    private JavaMailSender javaMailSender;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public boolean sendSms(String principal) {

        if(redisTemplate.hasKey("user:code:"+principal)){
            return false;
        }

        String code = RandomCodeUtil.create(6);
        //存入redis,
        redisTemplate.opsForValue().set("user:code:"+principal,code,90, TimeUnit.SECONDS);
        //连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GCuCN9EydDi8BkYXLRq", "FXb6LejbBuc63Iu0eOPAphSKgbXMV3");
        IAcsClient client = new DefaultAcsClient(profile);

        //构建请求
        CommonRequest request = new CommonRequest();

        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", principal);
        request.putQueryParameter("SignName", "857有贝口腔网");
        request.putQueryParameter("TemplateCode", "SMS_197890819");

        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(map));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());

            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean sendEmail(String principal) {

        if(redisTemplate.hasKey("user:code:"+principal)){
            return false;
        }

        String code = RandomCodeUtil.create(6);
        //存入redis,90s有效
        redisTemplate.opsForValue().set("user:code:"+principal,code,90, TimeUnit.SECONDS);

        SimpleMailMessage message=new SimpleMailMessage();
        //邮件设置
        message.setSubject("验证码");
        message.setText("【857有贝口腔网】\n 您的验证码为: "+code+" ，该验证码5分钟内有效，请勿泄漏于他人！");
        message.setTo(principal);
        message.setFrom("cn.lvsicheng@qq.com");
        javaMailSender.send(message);
        return true;
    }

    @Override
    public boolean sendUpdateSms(String userName,String type) {

        String value = RandomCodeUtil.create(6);

        if("0".equals(type)){
            redisTemplate.opsForValue().set("user:iphone:send:"+userName,value);
            redisTemplate.expire("user:iphone:send:"+userName,90, TimeUnit.SECONDS);
        }else if("1".equals(type)){
            redisTemplate.opsForValue().set("user:iphone:update:send:"+userName,value);
            redisTemplate.expire("user:iphone:update:send:"+userName,90, TimeUnit.SECONDS);
        }



        //连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GCuCN9EydDi8BkYXLRq", "FXb6LejbBuc63Iu0eOPAphSKgbXMV3");
        IAcsClient client = new DefaultAcsClient(profile);

        //构建请求
        CommonRequest request = new CommonRequest();

        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", userName);
        request.putQueryParameter("SignName", "857有贝口腔网");
        request.putQueryParameter("TemplateCode", "SMS_197890819");

        Map<String,Object> map = new HashMap<>();
        map.put("code",value);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(map));

        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean sendUpdateEmail(String userName,String type) {

        String value = RandomCodeUtil.create(6);
        if("0".equals(type)){
            redisTemplate.opsForValue().set("user:email:send:"+userName,value);
            redisTemplate.expire("user:email:send:"+userName,90, TimeUnit.SECONDS);
        }else if("1".equals(type)){
            redisTemplate.opsForValue().set("user:email:update:send:"+userName,value);
            redisTemplate.expire("user:email:update:send:"+userName,90, TimeUnit.SECONDS);
        }

        SimpleMailMessage message=new SimpleMailMessage();
        //邮件设置
        message.setSubject("验证码");
        message.setText("【857有贝口腔网】\n 您的验证码为: "+value+" ，该验证码5分钟内有效，请勿泄漏于他人！");
        message.setTo(userName);
        message.setFrom("cn.lvsicheng@qq.com");
        javaMailSender.send(message);
        return true;
    }
}
